<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\TongjiService;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class TongjiController extends BaseController
{
    //工时录入

    /** 
     * 个人报表
     * 所有人可见
     * */
    public function person(Request $request)
    {
        $pas['urole_id'] = $request->post('urole_id', 0);
        $pas['udepart_id'] = $request->post('udepart_id', 0);
        $pas['ustaff_id'] = $request->post('ustaff_id', 0);
        $pas['is_leader'] = $request->post('is_leader', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['staff_id'] = $request->post('uid', 0);
        $pas['year'] = $request->post('year_id', 0);
        $pas['month'] = $request->post('month_id', 0);
        $pas['sort_id'] = $request->post('sort_id', 0);
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        if (!$pas['year']) {
            $pas['year'] = date('Y', time());
        }
        if (!$pas['month']) {
            $pas['month'] = date('m', time());
        }
        $res = TongjiService::person($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data'], $res['total']);
    }

    // 个人导出
    public function staffMonthOut(Request $request)
    {
        $pas['urole_id'] = $request->post('urole_id', 0);
        $pas['udepart_id'] = $request->post('udepart_id', 0);
        $pas['ustaff_id'] = $request->post('ustaff_id', 0);
        $pas['is_leader'] = $request->post('is_leader', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['staff_id'] = $request->post('uid', 0);
        $pas['year'] = $request->post('year_id', 0);
        $pas['month'] = $request->post('month_id', 0);
        if (!$pas['year']) {
            $pas['year'] = date('Y', time());
        }
        if (!$pas['month']) {
            $pas['month'] = date('m', time());
        }
        $res = TongjiService::getOutData($pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        $fname = $res['data']['fname'];
        $cellData = $res['data']['data'];
        // ddd($cellData);
        Excel::create($fname,function($excel) use ($cellData){
            $excel->sheet('sheet', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xlsx');
    }

    /** 
     * 项目组
     * 部门经理看本部门的
     * 经理、管理员、审核员看所有的
     * */
    public function item(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['udepart_id'] = $request->post('udepart_id', 0);
        $pas['uid'] = $request->post('uid', 0);
        $pas['is_leader'] = $request->post('is_leader', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['staff_id'] = $request->post('staff_id', 0);
        $pas['year'] = $request->post('year', 0);
        $pas['month'] = $request->post('month_id', 0);
        $pas['sort_id'] = $request->post('sort_id', 0);
        if (!$pas['year']) {
            $pas['year'] = date('Y', time());
        }
        if (!$pas['month']) {
            $pas['month'] = date('m', time());
        }
        $res = TongjiService::item($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data'], $res['total']);
    }

    /** 
     * 所有项目
     * */
    public function all(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['staff_id'] = $request->post('staff_id', 0);
        $pas['year'] = $request->post('year', 0);
        $pas['month'] = $request->post('month_id', 0);
        $pas['sort_id'] = $request->post('sort_id', 0);
        if (!$pas['year']) {
            $pas['year'] = date('Y', time());
        }
        if (!$pas['month']) {
            $pas['month'] = date('m', time());
        }
        $res = TongjiService::all($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data'], $res['total']);
    }

    //环比
    public function huan(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['staff_id'] = $request->post('staff_id', 0);
        $pas['year'] = $request->post('year', 0);
        $pas['month'] = $request->post('month_id', 0);
        $pas['sort_id'] = $request->post('sort_id', 0);
        if (!$pas['year']) {
            $pas['year'] = date('Y', time());
        }
        if (!$pas['month']) {
            $pas['month'] = date('m', time());
        }
        $res = TongjiService::huan($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data'], $res['total']);
    }

    //图
    public function tu(Request $request)
    {
        $pas['year'] = $request->post('year_id', 0);
        $pas['month'] = $request->post('month_id', 0);
        if (!$pas['year']) {
            $pas['year'] = date('Y', time());
        }
        if (!$pas['month']) {
            $pas['month'] = date('m', time());
        }
        $res = TongjiService::tu($pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //选项
    public function getOption(Request $request)
    {
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $res = TongjiService::getOption($pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //项目选项
    public function itemOption(Request $request)
    {
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $res = TongjiService::itemOption($pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //测试导出
    public function excelTest(){
        $cellData = [
            ['学号','姓名','成绩'],
            ['10001','AAAAA','99'],
            ['10002','BBBBB','92'],
            ['10003','CCCCC','95'],
            ['10004','DDDDD','89'],
            ['10005','EEEEE','96'],
        ];
        Excel::create('学生成绩',function($excel) use ($cellData){
            $excel->sheet('score', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xlsx');
    }
}